﻿using Abp.EntityFrameworkCore;
using Magicodes.Admin.WULINGAddresss;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Magicodes.Admin.EntityFrameworkCore.Repositories
{
    public class WULINGAddressRepository: AdminRepositoryBase<WULINGAddress, long>, IWULINGAddressRepository
    {
        public WULINGAddressRepository(IDbContextProvider<AdminDbContext> dbContextProvider)
               : base(dbContextProvider)
        {

        }

        public async Task ImportFromExcel(List<WULINGAddress> wULINGAddresses)
        {
            var list = this.GetAll() ;
            var updates = new List<WULINGAddress>();
            foreach (var item in wULINGAddresses)
            {
                if (list != null && list.Any(s => s.ServiceStationCode == item.ServiceStationCode))
                {
                    var temp = list.First(s => s.ServiceStationCode == item.ServiceStationCode);
                    temp.ServiceStationName = item.ServiceStationName;
                    temp.PostCode = item.PostCode;
                    temp.Contact = item.Contact;
                    temp.ContactPhoneNumber = item.ContactPhoneNumber;
                    temp.ReceiverAddress = item.ReceiverAddress;
                    temp.ServiceStationManager = item.ServiceStationManager;
                    temp.ServiceStationManagerPhoneNumber = item.ServiceStationManagerPhoneNumber;
                    temp.Area = item.Area;
                    temp.Province = item.Province;
                    temp.City = item.City;
                    temp.District = item.District;
                    //updates.Add(temp);
                    continue;
                }
               await this.Context.WULINGAddresss.AddAsync(item);
            }

           await this.Context.SaveChangesAsync();
             
        }
    }
}
